package timing.ukulele.auth.security.user;

import com.alibaba.fastjson2.JSON;
import jakarta.annotation.Resource;
import lombok.Getter;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import timing.ukulele.auth.config.properties.AppProperties;
import timing.ukulele.auth.config.properties.SiteProperties;
import timing.ukulele.auth.http.wechat.WeChatExchange;
import timing.ukulele.auth.model.WxAppSessionResponse;
import timing.ukulele.auth.model.WxWeixinTokenResponse;
import timing.ukulele.auth.support.RedisOperator;
import timing.ukulele.common.dictionary.ThirdPartyTypeEnum;
import timing.ukulele.data.third.view.common.SysThirdPartyUserVO;
import timing.ukulele.facade.portal.feign.UserFeignFacade;
import timing.ukulele.facade.third.feign.SysThirdPartyUserFeignFacade;
import timing.ukulele.facade.third.feign.ThirdCommonFeignFacade;

@Service
@Import({SiteProperties.class, AppProperties.class})
public class UnionUserDetailService {
    private final RedisOperator<String> cacheManager;
    private final WeChatExchange weChatExchange;
    private final AppProperties appProperties;
    private final SiteProperties siteProperties;

    @Getter
    @Resource
    private UserFeignFacade userService;
    @Getter
    @Resource
    private ThirdCommonFeignFacade thirdCommonFeignFacade;
    @Getter
    @Resource
    private SysThirdPartyUserFeignFacade sysThirdPartyUserFeignFacade;

    public UnionUserDetailService(
            RedisOperator<String> cacheManager,
            WeChatExchange weChatExchange,
            AppProperties appProperties,
            SiteProperties siteProperties) {
        this.cacheManager = cacheManager;
        this.weChatExchange = weChatExchange;
        this.appProperties = appProperties;
        this.siteProperties = siteProperties;
    }

    /**
     * 获取微信小程序openid
     *
     * @param code code
     * @return openid
     */
    public String code2OpenIdByApp(String code) {
        if (!StringUtils.hasText(code)) {
            return null;
        }
        WxAppSessionResponse model = null;
        String result = this.weChatExchange.code2OpenIdByApp(appProperties.getAppid(), appProperties.getAppsecret(), "authorization_code", code);
        if (StringUtils.hasLength(result)) {
            model = JSON.parseObject(result, WxAppSessionResponse.class);
        }
        if (model != null && StringUtils.hasText(model.getUnionid()) && StringUtils.hasText(model.getOpenid())) {
            SysThirdPartyUserVO thirdPartyUserVO = new SysThirdPartyUserVO();
            thirdPartyUserVO.setPlatId(model.getOpenid());
            thirdPartyUserVO.setUnionId(model.getUnionid());
//            sysThirdPartyUserFeignFacade.updateThirdInfo(thirdPartyUserVO);
            cacheManager.set("session_key_" + model.getOpenid(), model.getSession_key());
            return model.getOpenid();
        }
        return null;
    }

    /**
     * 获取微信openid
     *
     * @param code code
     * @return openid
     */
    public String code2OpenIdByWeixin(String code) {
        if (!StringUtils.hasText(code))
            return null;
        WxWeixinTokenResponse model = null;
        String result = this.weChatExchange.code2OpenIdByWeixin(siteProperties.getAppid(), siteProperties.getSecret(), "authorization_code", code);
        if (StringUtils.hasLength(result)) {
            model = JSON.parseObject(result, WxWeixinTokenResponse.class);
        }
        if (model != null && StringUtils.hasLength(model.getOpenid()) && StringUtils.hasLength(model.getUnionid())) {
            SysThirdPartyUserVO thirdPartyUserVO = new SysThirdPartyUserVO();
            thirdPartyUserVO.setPlatId(model.getOpenid());
            thirdPartyUserVO.setUnionId(model.getUnionid());
            thirdPartyUserVO.setPlatSource(ThirdPartyTypeEnum.WX_WEB.getValue());
//            sysThirdPartyUserFeignFacade.addThirdInfo(thirdPartyUserVO);
            return model.getOpenid();
        }

        return null;
    }

}
